//
//  ABCViewController.m
//  gameXepHinh
//
//  Created by hoangha052 on 3/4/14.
//  Copyright (c) 2014 Hoangha. All rights reserved.
//

#import "ABCViewController.h"

@interface ABCViewController ()

@end

@implementation ABCViewController
{

}
@synthesize array;
//@synthesize arraybutton;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIButton *bt1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 50, 30)];
    [bt1 setBackgroundColor:[UIColor redColor]];
    bt1.tag = 1;
    [bt1 addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bt1];
    pos[0].x = bt1.center.x;
    pos[0].y = bt1.center.y;
    
    UIButton *bt2 = [[UIButton alloc] initWithFrame:CGRectMake(bt1.frame.origin.x+bt1.frame.size.width, bt1.frame.origin.y, 50, 30)];
    [bt2 addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchUpInside];
    [bt2 setBackgroundColor:[UIColor yellowColor]];
    bt2.tag = 2;
    [self.view addSubview:bt2];
    pos[1].x = bt2.center.x;
    pos[1].y = bt2.center.y;

    
    UIButton *bt3 = [[UIButton alloc] initWithFrame:CGRectMake(bt2.frame.origin.x+bt2.frame.size.width, bt1.frame.origin.y, 50, 30)];
    [bt3 setBackgroundColor:[UIColor blueColor]];
    bt3.tag = 3;
    [bt3 addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchUpInside];
    pos[2].x = bt3.center.x;
    pos[2].y = bt3.center.y;
    [self.view addSubview:bt3];
    
    UIButton *bt4 = [[UIButton alloc] initWithFrame:CGRectMake(bt1.frame.origin.x, bt1.frame.origin.y+bt1.frame.size.height, 50, 30)];
        [bt4 addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchUpInside];
    [bt4 setBackgroundColor:[UIColor greenColor]];
    bt4.tag = 4;
    pos[3].x = bt4.center.x;
    pos[3].y = bt4.center.y;
    [self.view addSubview:bt4];
    
    UIButton *bt5 = [[UIButton alloc] initWithFrame:CGRectMake(bt2.frame.origin.x, bt4.frame.origin.y, 50, 30)];
    bt5.tag = 5;
        [bt5 addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchUpInside];
    [bt5 setBackgroundColor:[UIColor brownColor]];
    pos[4].x = bt5.center.x;
    pos[4].y = bt5.center.y;
    [self.view addSubview:bt5];
    
    UIButton *bt6 = [[UIButton alloc] initWithFrame:CGRectMake(bt3.frame.origin.x, bt4.frame.origin.y, 50, 30)];
    [bt6 setBackgroundColor:[UIColor purpleColor]];
    [bt6 addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchUpInside];
    pos[5].x = bt6.center.x;
    pos[5].y = bt6.center.y;
    bt6.tag = 6;
    [self.view addSubview:bt6];

    array = [[NSArray alloc] initWithObjects:bt1,bt2,bt3,bt4,bt5,bt6, nil];
    
}

-(void) moveButton:(id) sender
{
    UIButton *button = (UIButton *) sender;
    NSLog(@"%d", button.tag);
    
    CGPoint pointSelectec = button.center;
    for ( int i =0; i<5; i++) {
        if (CGPointEqualToPoint(pointSelectec, pos[i])) {
            NSLog(@"day la vi tri %d", i);
            [self checkPos:i withButton:button];
            break;
        }
    }
}

- (void) checkPos:(NSInteger ) index withButton:(UIButton *)_button
{
      NSLog(@"%d",_button.tag);

    for (UIButton *button in array) {
        if (CGPointEqualToPoint(pos[index+1],button.center)) {
            NSLog(@"%d",button.tag);
            if (button.tag == 6) {
                [self swapPos:button andPos:_button];
            }
        }

    }
}

- (void) swapPos:(UIButton* )btn1 andPos:(UIButton *)btn2
{
//    NSLog(@"%d", btn1.tag);
//    NSLog(@"%d", btn2.tag);
//    btn1.center = pos[btn2.tag];
//    btn2.center = pos[btn1.tag];
    CGRect frame = btn1.frame;
    btn1.frame = btn2.frame;
    btn2.frame = frame;
    

}
- (BOOL) comparePoint:(NSInteger ) point1
{
    NSArray *subviews =  [self.view subviews];
    
    for (UIButton *buton in  subviews) {
        
        if (CGPointEqualToPoint(buton.center, pos[point1]) && buton.tag == 5) {
            return YES;
        }
    }
    return NO;
}
- (void) hoanvi
{
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
